package com.showcai.controller.organization;

import com.github.pagehelper.PageInfo;
import com.showcai.controller.base.BaseController;
import com.showcai.def.Constant;
import com.showcai.module.*;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2018-9-6.
 */
@RequestMapping("/organization")
@Controller
public class OrganizationController extends BaseController {

    @Autowired
    OrganizationService organizationService;


    @RequestMapping(value = "/getOrganizationList")
    @ResponseBody
    public ModelAndView getOrderList(Integer page, Integer pageSize) {
        ModelAndView mov = new ModelAndView();
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        Map map = new HashMap<String,String>();
        map.put("page",String.valueOf(page));
        map.put("pageSize",String.valueOf(pageSize));
        PageInfo list =  organizationService.organizationPageList(map);
        mov.addObject("pageinfo",list);
        mov.setViewName("/organization/organizationList");
        return mov;
    }
    @RequestMapping(value = "/organizationAdd")
    @ResponseBody
    public ModelAndView organizationAdd(Long tid) {
        ModelAndView mov = new ModelAndView();
        Organization organization = organizationService.selectOrganization(tid);
        mov.setViewName("/organization/organization");
        mov.addObject("organization",organization);
        return mov;
    }
    @RequestMapping(value = "/modifyOrganization")
    @ResponseBody
    public String modifyOrganization(Organization organization) {
        ResultObj resultObj = new ResultObj();
        organizationService.modifyOrganization(organization);
        resultObj.setCode(ReturnCode.SUCCESS.getCode());
        resultObj.setMsg(ReturnCode.SUCCESS.getMsg());
        return resultObj.toJson();
    }
    @RequestMapping(value = "/deleteOrganization")
    @ResponseBody
    public String deleteOrganization(Organization organization) {
        ResultObj resultObj = new ResultObj();
        organizationService.deleteOrganization(organization);
        resultObj.setCode(ReturnCode.SUCCESS.getCode());
        resultObj.setMsg(ReturnCode.SUCCESS.getMsg());
        return resultObj.toJson();
    }
    @RequestMapping(value = "/operateOrganization")
    @ResponseBody
    public String operateOrganization(Long tid) {
        ResultObj resultObj = new ResultObj();
        organizationService.operateOrganization(tid);
        resultObj.setCode(ReturnCode.SUCCESS.getCode());
        resultObj.setMsg(ReturnCode.SUCCESS.getMsg());
        return resultObj.toJson();
    }
    @RequestMapping(value="/resetPassword", method = RequestMethod.POST)
    @ResponseBody
    public String resetPassword(Long tid) {
        ResultObj resultObj = new ResultObj();
        organizationService.resetPassword(tid);
        resultObj.setCode(ReturnCode.SUCCESS.getCode());
        resultObj.setMsg(ReturnCode.SUCCESS.getMsg());
        return resultObj.toJson();
    }


}
